import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/life/DizhiGuanli.dart';
import 'package:shared_preferences/shared_preferences.dart';
class EditAddress extends StatefulWidget {
  EditAddress({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _EditDizhi createState() => _EditDizhi();
}

class _EditDizhi extends State<EditAddress> {
  _EditDizhi(){
    _onLoading();
  }
  String name='';
  String phone='';
  String address='';
  String id='';

  TextEditingController _unameController = new TextEditingController(); // 用户用户名从这里获取
  TextEditingController _telController = new TextEditingController();
  TextEditingController _dizhiController = new TextEditingController();
  bool isDefault=true;

  // 加载数据的函数
  void _onLoading() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    id = prefs.getString("dizhiId");
    // 发起请求
    // Response response = await Dio().get("http://xllm.szghrj.com:8082/app/address/searchUserAddress",queryParameters:{
    //   "userId":userId
    // });
    // var x = json.decode(response.toString());
    var x = await HttpUtil().get("/app/address/getAddressById?id=${id}");
    if(x['code']==200){
      name=x['data']['adName'];
      phone=x['data']['adPhone'];
      address=x['data']['addressDetail'];
      isDefault=x['data']['adStatus']==1 ? true : false;
    }
    // if failed,use loadFailed(),if no data return,use LoadNodata()
    if(mounted)
      setState(() {
        _unameController=new TextEditingController(text: name);
        _telController=new TextEditingController(text: phone);
        _dizhiController=new TextEditingController(text: address);
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("修改收货地址"),
      ),
      body: Container(
        child: Column(
          // mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              child: TextFormField(
                obscureText: false,
                controller:_unameController ,
                decoration: InputDecoration(
                  labelText: "收件人姓名",
                ),
                onSaved: (str) {
                  _unameController.text = str;//str为文本的内容值
                },
              ),
            ),
            Container(
              child: TextFormField(
                controller: _telController,
                obscureText: false,
                decoration: InputDecoration(
                  labelText: "收件人电话",
                ),
                onSaved: (str) {
                  _telController.text = str;//str为文本的内容值
                },
              ),
            ),
            Container(
              child: TextFormField(
                controller: _dizhiController,
                obscureText: false,
                decoration: InputDecoration(
                  labelText: "详细收货地址",
                ),
                onSaved: (str) {
                  _dizhiController.text = str;//str为文本的内容值
                },
              ),
            ),
            Container(
                child: Row(
                  children: [
                    Checkbox(value: isDefault, onChanged: (val){
                      isDefault=val;
                      setState(() {

                      });
                    },),
                    Container(child: Text("默认收货地址"),margin: EdgeInsets.only(right: 20),),
                    RaisedButton(
                      color: Colors.blue,
                      shape: RoundedRectangleBorder(
                      ),
                      onPressed: () async {
                        // post提示用户名或密码为空，get提交可以登录
                        int adStatus = 0;
                        if(isDefault){
                          adStatus = 1;
                        }
                        var x = await HttpUtil().get("/app/address/editAddress?id=${id}&addressDetail=${_dizhiController.text.trim()}&adName=${_unameController.text.trim()}&adPhone=${_telController.text.trim()}&adStatus=${adStatus}");
                        if(x['code']==200){
                          Fluttertoast.showToast(
                              msg: x['msg'],
                              toastLength: Toast.LENGTH_SHORT,
                              gravity: ToastGravity.BOTTOM,
                              timeInSecForIosWeb: 1,
                              backgroundColor: Colors.black45,
                              textColor: Colors.white,
                              fontSize: 16.0
                          );
                          List value=[_dizhiController.text.trim(),_unameController.text.trim(),_telController.text.trim(),adStatus];
                          MyRoute.pop(value);
                        }else{
                          Fluttertoast.showToast(
                              msg: x['msg'],
                              toastLength: Toast.LENGTH_SHORT,
                              gravity: ToastGravity.BOTTOM,
                              timeInSecForIosWeb: 1,
                              backgroundColor: Colors.black45,
                              textColor: Colors.white,
                              fontSize: 16.0
                          );
                        }
                      },
                      child: Text("确定",),
                    ),
                  ],
                )
            ),
          ],
        ),
      ),
    );
  }
}